//
//  LoginViewController.swift
//  DriverSchoolAPP
//
//  Created by Logel on 2017/5/23.
//  Copyright © 2017年 Logel. All rights reserved.
//

import UIKit

var user = User()
class LoginViewController: UIViewController,LgCallBack {
    @IBOutlet weak var phoneLbl: UITextField!
    @IBOutlet weak var pwdLbl: UITextField!
    var fromWhere = ""
    override func viewDidLoad() {
        super.viewDidLoad()
        switch fromWhere {
        case "Mine":
            let leftBarBtn = UIBarButtonItem(title: "返回", style: .plain, target: self,
                                             action: #selector(goBack))
            self.navigationItem.leftBarButtonItem = leftBarBtn
        default:
            break
        }
        // Do any additional setup after loading the view.
    }
    

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    @IBAction func doLogin(_ sender: UIButton) {
        if (phoneLbl.text! == "" || pwdLbl.text == "") {
            showMsg(msg: "用户名或密码为空")
        }else{
            asyn_req_post(method: M_Login, cb: self, p: ["username": phoneLbl.text! as AnyObject ,"password":pwdLbl.text! as AnyObject])
        }
        
    }
    
    
    func callBack(method: String, data: AnyObject) {
        let json = JSON(data)
        print(json)
        if json["info"]["accountId"].intValue > 0 {
            user = parseUser(x: json)
            print(user)
            setNormalDefault(key: "token", value: user.token as AnyObject?)
            self.navigationController!.popToRootViewController(animated: true)
        }
        else{
            showMsg(msg: "账号未注册或者密码错误")
        }
       
    }
    
    func goBack(){
        // 获得视图控制器中的某一视图控制器
        performSegue(withIdentifier: "toMain", sender: nil)
    }
  

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
